package qumaimai.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import qumaimai.dao.UserDao;
import qumaimai.entity.Goods;
import qumaimai.entity.User;
import qumaimai.util.JdbcUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public User login(String username, String password) {
		User user = null;
		Connection conn = JdbcUtil.getConnection();
		String sql = "select * from t_user where  account='"+username+"' and password='"+password+"'";
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){				
				//һ
				int id = rs.getInt("id");
				String account = rs.getString("account");
				String nickName = rs.getString("nick_name");
				String password2 = rs.getString("password");
				int gender = rs.getInt("gender");
				String idCardNo = rs.getString("id_card_no");
				String email = rs.getString("email");
				String phone = rs.getString("phone");
				int type = rs.getInt("type");
				String face = rs.getString("face");
				int isLock = rs.getInt("is_lock");
				user = new User(id, account, nickName, password2, gender, idCardNo, email, phone, type, isLock, face);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public int add(String account, String password, String email, String phone) {
		String sql = "insert into t_user(account,password,nick_name,email,phone) values('"+account+"','"+password+"','"+account+"Сɰ','"+email+"','"+phone+"')";
		return JdbcUtil.update(sql);
	}

	@Override
	public int checkAccount(String account) {
		int n = 0;
		Connection conn = JdbcUtil.getConnection();
		String sql = "select count(*) from t_user where account='"+account+"'";
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return n;
	}

	@Override
	public int checkPhone(String phone) {
		int n = 0;
		Connection conn = JdbcUtil.getConnection();
		String sql = "select count(*) from t_user where phone='"+phone+"'";
		System.out.println(sql);
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int checkEmail(String email) {
		int n = 0;
		String sql = "select count(*) from t_user where email='"+email+"'";
		System.out.println(sql);
		try {
			Connection conn = JdbcUtil.getConnection();
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int editNickName(String nickname,int id) {
		String sql = "update t_user set nick_name='"+nickname+"' where id="+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int editPhone(String phone,int id) {
		String sql = "update t_user set phone='"+phone+"' where id="+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int editEmail(String email,int id) {
		String sql = "update t_user set email='"+email+"' where id="+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int editPassword(String password,int id) {
		String sql = "update t_user set password='"+password+"' where id="+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int editFace(String face, int id) {
		String sql = "update t_user set face='"+face+"' where id = "+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public List<User> findAll(int page, int size, String account, String phone) {
		int n = (page-1)*size;
		List<User> list = new ArrayList<User>();
		String sql = "select * from t_user where 1=1";
		if(account!=null&&account!=""){
			sql+=" and account='"+account+"'";
		}
		if(phone!=null&&phone!=""){
			sql+=" and phone='"+phone+"'";
		}
		sql +=" limit "+n+","+size;
		System.out.println(sql);
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String account2 = rs.getString("account");
				String nickName = rs.getString("nick_name");
				int gender = rs.getInt("gender");
				String idCardNo = rs.getString("id_card_no");
				String email = rs.getString("email");
				String phone2 = rs.getString("phone");
				int type = rs.getInt("type");
				int isLock = rs.getInt("is_lock");
				String face = rs.getString("face");
				User user = new User(id, account2, nickName, null, gender, idCardNo, email, phone2, type, isLock, face);
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public int getCount(String account, String phone) {
		int n = 0;
		String sql = "select count(*) from t_user where 1=1";
		if(account!=null&&account!=""){
			sql+=" and account='"+account+"'";
		}
		if(phone!=null&&phone!=""){
			sql+=" and phone='"+phone+"'";
		}
		System.out.println(sql);
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int lock(int id) {
		String sql = "update t_user SET is_lock=abs(is_lock-1) where id="+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int reset(int id) {
		String sql = "update t_user SET password='123456' where id="+id;
		return JdbcUtil.update(sql);
	}




}